package com.module.product.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.util.Page;
import base.util.StringUtil;

import com.module.product.vo.AttrGroup;
/**
*
* 商品属性组dao类
*
* @author  王勇云
* @version  [V1.0, Jul 22, 2011]
* @see  [相关类/方法]
* @since  [二代服务平台/商品属性组]
*/
public class AttrGroupDao extends GenericHibernateDao<AttrGroup, String>{

	private static final AttrGroupDao instance = new AttrGroupDao();
	
	private AttrGroupDao(){}
	
	public synchronized static AttrGroupDao getInstance() {
		return instance;
	}
	
	/**
	 * <逻辑删除>
	 * <功能详细描述>
	 * @param attrGroup
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
    public boolean deleteObjs(Session session,AttrGroup obj){
        boolean flag = false;
        try {
            String hql = "update AttrGroup set updaterId = ?,updateTime=?,status =? where id in ("+obj.getId()+")";
            Query query  = session.createQuery(hql); 
                  query.setParameter(0,obj.getUpdaterId());
                  query.setParameter(1, new Date());
                  query.setParameter(2, CUDEnum.D.toString());
                  int i  = query.executeUpdate();
            flag = true;
        } catch (Exception ex) {
            ex.printStackTrace();
        } 
        return flag;
    }

	
    public List<AttrGroup> listAll(Session session){
        List<AttrGroup> list  = new ArrayList();
        Query query = session.createQuery("from AttrGroup as obj where obj.status <>? ");
        query.setParameter(0,CUDEnum.D.toString());
        list = query.list();
        return list;
    }

    
    public Page getList(Session session,AttrGroup obj,Page page){
    	List<AttrGroup> list = null ;
    	List listR = new ArrayList();
    	// List<AttrGroup> childrenList = null;
        String hql1 = "from AttrGroup as obj where obj.status !='D' and obj.pid='0' ";
        String hql2 = "select count(*) from AttrGroup as obj  where obj.status !='D' and obj.pid='0' ";
        if(StringUtils.isNotBlank(obj.getName())){
            hql1=hql1 +  " and obj.name like '%"+obj.getName()+"%'";
            hql2=hql2 +  " and obj.name like '%"+obj.getName()+"%'";
        }
        if(!StringUtil.isNull(obj.getCategoryId())){
        	hql1=hql1 +  " and obj.categoryId = '"+obj.getCategoryId()+"'";
            hql2=hql2 +  " and obj.categoryId = '"+obj.getCategoryId()+"'";
        }
        hql1 += "order by obj.createTime desc";
        Query query = session.createQuery(hql1);
        if(page.getLimit()!=-1)//-1表示不分页。
        {
         query.setFirstResult(page.getStart()).setMaxResults(page.getLimit());
        
        page.setTotal((long) ((Long) session.createQuery(hql2)
            .uniqueResult()).intValue());
        }
        list=query.list();
        for(AttrGroup c :list){
        	c.setCategoryName(this.getCategoryName(session, c.getCategoryId()));
        	listR.add(c);
        }  
        page.setRows(listR);
        return page;
    } 
   
//    public List<AttrGroup> getAttrGroupTree(Session session,String pid){
//        List<AttrGroup> list = null ;
//        List<AttrGroup> childrenList = null;
//        Query query = session.createQuery("from AttrGroup as obj where obj.status <>? and pid=?");
//              query.setParameter(0,CUDEnum.D.toString());
//              query.setParameter(1,pid);
//              list = query.list();  
//            for(AttrGroup c :list){
//                  childrenList = getAttrGroupTree(session,c.getId());
//                  c.setChildren(childrenList.size()==0?null:childrenList);
//            }    
//     return list;
//    }
    
    private String  getCategoryName(Session session,String id){
    	 Query query = session.createQuery("select name from Category  where status <>? and id=?");
    	 query.setParameter(0,CUDEnum.D.toString());
         query.setParameter(1,id);
         List<String> list = query.list();  
         if(list != null && !list.isEmpty()){
        	 return list.get(0);
         }
         return null;
    }

    public List<AttrGroup> getAttrGroupRootList(Session session,String pid){
        List<AttrGroup> list = null ;
        Query query = session.createQuery("from AttrGroup as obj where obj.status <>? and pid=?");
              query.setParameter(0,CUDEnum.D.toString());
              query.setParameter(1,pid);
              list = query.list();  
     return list;
    }
    
    public List<AttrGroup> getAttrGroupTreeOne(Session session,String id)
    {
    	 List<AttrGroup> list = null ;
    	// List<AttrGroup> childrenList = null;
         Query query = session.createQuery("from AttrGroup as obj where obj.status <>? and id=?");
               query.setParameter(0,CUDEnum.D.toString());
               query.setParameter(1,id);
               list = query.list();  
//               for(AttrGroup c :list){
//                // childrenList = getAttrGroupTree(session,c.getId());
//                // c.setChildren(childrenList.size()==0?null:childrenList);
//           } 
          return list;
    }
    
    public AttrGroup findAttrGroupByName(Session session,String categoryId,String name){
    	String hql="FROM AttrGroup AS obj WHERE obj.status !='D' and obj.categoryId='"+categoryId+"' AND obj.name='"+name+"'";
    	Query query=session.createQuery(hql);
    	List list=query.list();
    	AttrGroup attrGroup;
    	if(list!=null&&list.size()>0){
    		attrGroup=(AttrGroup)list.get(0);
    	}else{
    		attrGroup=null;
    	}
    	return attrGroup;
    }
    
    

    
    public boolean updateAttrNumbyId(Session session,String id,int num){
        boolean b = false;
        try
        {
            AttrGroup obj = get(session,id);
            int i = Integer.parseInt(StringUtils.isBlank(obj.getAttrNum())?"0":obj.getAttrNum());
            String  str = String.valueOf(i+num);
            obj.setAttrNum(str);
            this.update(session,obj); 
            b = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return b;
    }
    /**
     * 根据categoryId统计该类别下的属性组
     * @param session
     * @return
     */
    public List getCateAttrGroupList(Session session){
 	   List list  = new ArrayList();
        Query query = session.createQuery("Select categoryId ,count(id)from AttrGroup as obj where obj.status <>? group by categoryId");
        query.setParameter(0,CUDEnum.D.toString());
        list = query.list();
        return list;
    }
    
}
